package 数组.easy;

/**
 * @author Lw中
 * @date 2020/9/13 10:10
 */
public class ErLingJiu {

    /**
     * 长度最小的子数组
     * @param s
     * @param nums
     * @return
     */
    public static int minSubArrayLen(int s, int[] nums) {
        int n = nums.length;
        int start = 0, end = 0, sum = 0, ans = Integer.MAX_VALUE;
        while (end < n) {
            sum += nums[end];
            // 若sum大于等于s，起始下标往右移
            while (sum >= s) {
                ans = Math.min(ans, end - start + 1);
                sum -= nums[start];
                start++;
            }
            // 负责的话，end下标右移
            end++;
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }

    public static void main(String[] args) {
        int[] arr = {2,3,1,2,4,3};
        int i = minSubArrayLen(7, arr);
        System.out.println(i);
    }

}
